#include "Circle.h"

NS_B2D_BEGIN

const Circle Circle::EMPTY(0,0,0);

Circle::Circle(void)
	: radius(0)
	, center(0,0)
{
}

Circle::Circle(const vec2& c, float r)
	: radius(r)
	, center(c)
{
}

Circle::Circle(float cx, float cy, float r)
	: radius(r)
	, center(cx, cy)
{
}

Circle::Circle(const Circle& c)
	: radius(c.radius)
	, center(c.center)
{
}
	
Circle& Circle::operator= (const Circle& c)
{
	radius = c.radius;
	center.x = c.center.x;
	center.y = c.center.y;
	return *this;
}

void Circle::include(const Circle& c)
{
	// Check for empty circles
	if( c.radius <= 0 ) return;
	if( radius <= 0 ) *this = c;

	float xmin = std::min(center.x - radius, c.center.x - c.radius);
	float xmax = std::max(center.x + radius, c.center.x + c.radius);
	float ymin = std::min(center.y - radius, c.center.y - c.radius);
	float ymax = std::max(center.y + radius, c.center.y + c.radius);

	radius = std::max(xmax-xmin, ymax-ymin);
	center = vec2(.5f*(xmin+xmax), .5f*(ymin+ymax));
}

bool Circle::insersects(const Circle& r) const
{
	// Check for empty circles
	if(r.radius <= 0 || radius <= 0) return false;

	float dist_lim2 = pow((radius+r.radius), 2);
	float dist2 = length2(center - r.center);
	return dist2 <= dist_lim2;
}

NS_B2D_END